project (rotor-light-playground-stm32
    LANGUAGES CXX C ASM
)

cmake_minimum_required(VERSION 3.8)
cmake_policy(SET CMP0069 NEW)

include(CheckIPOSupported)
check_ipo_supported(RESULT supported OUTPUT error)
if( supported )
    message(STATUS "IPO / LTO enabled")
    set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
else()
    message(STATUS "IPO / LTO not supported: <${error}>")
endif()

if ("${TOOLCHAIN_PREFIX}" STREQUAL "")
    include(CTest)
    add_subdirectory("tests")
else()
    add_subdirectory("libs/rotor-light")
    target_include_directories(rotor_light PUBLIC "${LIBSTDCPP_HOME}/include")

    set(LINKER_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/src/STM32F407VGTx_FLASH.ld)

    add_executable(sample
        src/CMSIS/Device/ST/STM32F4xx/Source/Templates/system_stm32f4xx.c
        src/CMSIS/Device/ST/STM32F4xx/Source/Templates/gcc/startup_stm32f407xx.s
        src/Supervisor.cpp
        src/main.cpp
        src/messages.cpp
        src/actor/DHT.cpp
        src/actor/Uart.cpp
        src/hw/board.cpp
        src/hw/timer9.cpp
        src/model/DHT11.cpp
    )

    target_link_libraries(sample rotor_light)
    target_include_directories(sample PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}/src
        ${CMAKE_CURRENT_SOURCE_DIR}/src/CMSIS/Device/ST/STM32F4xx/Include/
        ${CMAKE_CURRENT_SOURCE_DIR}/src/CMSIS/Include/
    )
    target_compile_definitions(sample PUBLIC
        STM32F407xx
        HSE_VALUE=8000000
    )
    target_link_options(sample PUBLIC -T ${LINKER_SCRIPT})
endif()
